<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>订单详情</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="/node_modules/LobsterUIFrame/weui/css/weui.css" />
    <link rel="stylesheet" href="/node_modules/LobsterUIFrame/weui/css/weuix.css" />
    <script src="/node_modules/LobsterUIFrame/weui/js/zepto.min.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/zepto.weui.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/swipe.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/macy.js"></script>
    <script src="/node_modules/LobsterUIFrame/weui/js/php.js"></script>
    <script src="/lobstersdk/lobster.js"></script>
    <script src="/jslib/template-web.js"></script>
    <style>
        .bottom {
            background-color: #F5F5F5;
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            align-items: center;
            display: flex;
            padding: 10px 4px;
            z-index: 2;
        }

        .btn {
            height: 40px;
            line-height: 40px;
        }
    </style>
</head>

<body ontouchstart>

</body>

</html>
<script type="text/template" id="ordertemp">
     <div class="weui-cells__title">收货地址</div>
    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>{{address.Name}} | {{address.TelePhone}}</p>
                <p>{{address.Province}}{{address.City}}{{address.Area}} {{address.Address}}</p>
            </div>
        </div>
    </div>
    <div class="weui-cells__title">商品信息</div>
    <div class="weui-panel__bd" id="goods">
        {{each goodslist item}}
        <a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg">
            <div class="weui-media-box__hd">
                <img class="weui-media-box__thumb" src="{{item.SpecImg}}" alt="">
            </div>
            <div class="weui-media-box__bd">
                <h4 class="weui-media-box__title">{{item.GoodsName}}</h4>
                <p class="weui-media-box__desc">规格 {{item.SpecName}}</p>
                <div class="weui-media-box__desc" style="display: flex;"> ￥{{item.GoodsPrice}}<div style="flex:1;text-align:right">
                    </div>x{{item.GoodsNum}}
                    {{if(item.ReturnGoodsNum>0)}}
                    <span> (已退{{item.ReturnGoodsNum}}件)</span>
                    {{/if}}
                    </div>
            </div>
        </a>
        {{if(order.OrderStatus==4&&item.ReturnGoodsNum<item.GoodsNum)}}
        <div style="display: flex;justify-content: flex-end;">
            <div>
                {{if(item.RefundIng==1)}}
                退款中
                {{else}}
                <a href="javascript:;" data-item="{{item}}" class="weui-btn weui-btn_mini weui-btn_primary bg-orange toAfterSale">
                    申请售后
                </a>
                {{/if}}
            </div>
        </div>
        {{/if}}
        {{/each}} 
    </div>
    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>商品总额</p>
            </div>
            <div class="weui-cell__ft">{{order.TotalFee}}</div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>运费</p>
            </div>
            <div class="weui-cell__ft">0.00</div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>店铺优惠</p>
            </div>
            <div class="weui-cell__ft">0.00</div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>实际支付</p>
            </div>
            <div class="weui-cell__ft">{{order.TotalFee}}</div>
        </div>
    </div>
    <div class="weui-cells__title">订单信息</div>
    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>订单编号</p>
            </div>
            <div class="weui-cell__ft">{{order.OrderNo}}</div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>下单时间</p>
            </div>
            <div class="weui-cell__ft">{{(order.CreateTime||"").substring(0,19)}}</div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>支付方式</p>
            </div>
            <div class="weui-cell__ft">{{order.PayTypetxt}}</div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>订单状态</p>
            </div>
            <div class="weui-cell__ft">{{order.OrderStatustxt}}</div>
        </div>
    </div>
    {{if(logistics.LogisticsNo!=null)}}
    <div class="weui-cells__title">物流信息</div>
    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>物流公司</p>
            </div>
            <div class="weui-cell__ft">{{logistics.LogisticsCompany}}</div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>物流单号</p>
            </div>
            <div class="weui-cell__ft">{{logistics.LogisticsNo}}</div>
        </div>
    </div>
    {{/if}}
    <div style="height:60px;"></div>
    {{if (order.OrderStatus==0||order.OrderStatus==1||order.OrderStatus==2||order.OrderStatus==4)}}
    <div class="bottom">
        <div style="flex:1;text-align:right;" data-orderno={{order.OrderNo}} data-totalfee="{{order.TotalFee}}">
            {{if(order.OrderStatus==0)}}
            <a href="javascript:;" class="btn weui-btn weui-btn_mini weui-btn_primary bg-orange topay"></i>去支付</a>
            {{else if(order.OrderStatus==1)}}
            <a href="javascript:;" class="btn weui-btn weui-btn_mini weui-btn_primary bg-orange torefund"></i>退款</a>
            {{else if(order.OrderStatus==2)}}
            <a href="javascript:;" class="btn weui-btn weui-btn_mini weui-btn_primary togetgoods"></i>确认收货</a>
            {{else if(order.OrderStatus==4)}}
            <a href="javascript:;" class="btn weui-btn weui-btn_mini weui-btn_primary toevaluate"></i>评价</a>
            {{/if}}
        </div>
    </div>
    {{/if}}
</script>

<script>

    lobsterh5.main({
        data: {
            order: {}, //订单详情
            address: {}, //订单收件人
            goodslist: [], //商品列表
            orderStatusDict: [{ "Code": "0", "Name": "待付款", "Pym": "DFK", "Wbm": "TWF", "Szm": "" },
            { "Code": "1", "Name": "待发货", "Pym": "DFH", "Wbm": "TVW", "Szm": "" },
            { "Code": "2", "Name": "待收货", "Pym": "DSH", "Wbm": "TNW", "Szm": "" },
            { "Code": "3", "Name": "待评价", "Pym": "DPJ", "Wbm": "TYW", "Szm": "" },
            { "Code": "4", "Name": "已完成", "Pym": "YWC", "Wbm": "NPD", "Szm": "" },
            { "Code": "5", "Name": "已取消", "Pym": "YQX", "Wbm": "NBI", "Szm": "" },
            { "Code": "6", "Name": "退款中", "Pym": "TKZ", "Wbm": "OFK", "Szm": "" },
            { "Code": "7", "Name": "已退款", "Pym": "YTK", "Wbm": "NOF", "Szm": "" }],
            payTypeDict: [{ "Code": "1", "Name": "微信支付", "Pym": "WXZF", "Wbm": "TWFW", "Szm": "" },
            { "Code": "2", "Name": "支付宝支付", "Pym": "ZFBZF", "Wbm": "FWPFW", "Szm": "" },
            { "Code": "3", "Name": "零钱支付", "Pym": "LQZF", "Wbm": "FQFW", "Szm": "" },
            { "Code": "4", "Name": "组合支付", "Pym": "ZHZF", "Wbm": "XWFW", "Szm": "" }],
            orderNo: "",
        },
        //初始化页面
        pageload: function () {
            this.data.orderNo = lobsterh5.GetUrlParam("orderNo");
            this.getalldata();
            this.initevent();
        },
        //初始化事件
        initevent: function () {
            var self = this;
            $(document).on("click", ".topay", function () {
                lobsterh5.CALLOUTSERVICE("pay.pay.preorder", {
                    SubMchId: '20220505085609644323',
                    BusinessOrderNo: self.data.order.OrderNo,
                    OrderType: 3,//业务类型 1预约挂号 2自助缴费
                    PayType: 1,//默认1 微信支付
                    Price: parseFloat(self.data.order.TotalFee) * 100, //单位分
                    Body: '商品购买',//商品描述
                    Memo: "商品购买"//self.data.goods.map(n => n.GoodsName).join(";")//可以不填
                }).then(res => {
                    $.hideLoading();
                    location = res.payredirecturl;
                }).catch(res => {
                    $.toast(res.msg, 'forbidden');
                });
            })
            $(document).on("click", ".torefund", function () {
                $.confirm("提示", "确定退款吗？", function () {
                    $.showLoading("退款中");
                    lobsterh5.POST("/lowcode/APP20200616102057595/shop/?fun=mall.refund", {
                        refund: JSON.stringify({
                            OrderNo: self.data.order.OrderNo,
                            RefundFee: self.data.order.TotalFee,
                            RefundType: 3,
                            CargoStatus: 1,
                            RefundDesc: '用户取消购买',
                            UserId: localStorage.getItem("userId")
                        }),
                        goods: JSON.stringify(self.data.goodslist),
                    }).then(res => {
                        $.hideLoading();
                        lobsterh5.CALLOUTSERVICE("wallet.wallet.transactionrefund", {
                            SubMchId: '20220505085609644323',
                            BusinessOrderNo: self.data.order.OrderNo,
                            BusinessRefundNo: res.refundNo,
                            PayType: 1,//默认1 微信支付
                            RefundRealFee: parseFloat(self.data.order.TotalFee) * 100, //单位分
                            RefundDesc: '用户取消购买',//商品描述 
                        }).then(res => {
                            location.href = "../refund/list.html";
                        }).catch(res => {
                            $.toast(res.msg, 'forbidden');
                        });
                    }).catch(res => {
                        $.toast(res.msg, 'forbidden');

                    })
                }, function () {
                    //取消操作
                });
            })
            $(document).on("click", ".togetgoods", function () {
                $.confirm('提示', '确定收货吗？', function () {
                    $.showLoading("提交中");
                    lobsterh5.GET("/lowcode/APP20200616102057595/shop/?fun=mall.UpdateOrderStatus", {
                        orderNo: self.data.order.OrderNo,
                        orderStatus: 4
                    }).then(res => {
                        $.hideLoading();
                        self.getalldata();
                    }).catch(res => {
                        $.toast(res.msg, 'forbidden');

                    })
                }, function () {
                    //取消操作
                });
            })
            $(document).on("click", ".toevaluate", function () {
                if (self.data.goodslist.length > 1) {
                    location.href = "../goods/evaluatelist.html?orderNo=" + self.data.order.OrderNo;
                } else {
                    location.href = "../goods/evaluate.html?goods=" + JSON.stringify(self.data.goodslist[0]);
                }
            })
            $(document).on("click", ".toAfterSale", function () {
                var item = $(this).data("item")
                if (item.RefundIng == 1) {
                    location.href = "../refund/detail.html?refundNo=" + item.RefundNo;
                } else {
                    location.href = "../refund/selectaftersale.html?goods=" + JSON.stringify([item]);
                }
            })
        },
        getalldata: function () {
            var self = this;
            Promise.all([self.getorderinfo(), self.getrefundlistbyorderno(),
            self.getorderaddressbyorderno(), self.getordergoodslist(), self.getLogistics()]).then(res => {
                self.data.order = res[0].data;
                self.data.refundlist = res[1].refundlist;
                self.data.address = res[2].address;
                self.data.goodslist = res[3].goodslist;
                self.data.goodslist.forEach(a => {
                    var filter = self.data.refundlist.filter(b => {
                        return b.GoodsId == a.GoodsId && b.SpecDetailId == a.SpecDetailId &&
                            '0,1,2,3'.indexOf(b.RefundStatus) > -1;
                    })
                    if (filter.length > 0) {
                        a.RefundIng = 1;
                        a.RefundNo = filter[0].RefundNo;
                    } else {
                        a.RefundIng = 0;
                        a.RefundNo = '';
                    }
                    var returnfilter = self.data.refundlist.filter(b => {
                        return b.GoodsId == a.GoodsId && b.SpecDetailId == a.SpecDetailId && b
                            .RefundStatus == 4;
                    })
                    if (returnfilter.length) {
                        a.ReturnGoodsNum = self.sum(returnfilter.map(n => parseInt(n.GoodsNum)));
                    } else {
                        a.ReturnGoodsNum = 0
                    }
                })
                var filter = self.data.orderStatusDict.filter(a => {
                    return a.Code == self.data.order.OrderStatus;
                })
                if (filter.length > 0)
                    self.data.order.OrderStatustxt = filter[0].Name;
                filter = self.data.payTypeDict.filter(a => {
                    return a.Code == self.data.order.PayType;
                })
                if (filter.length > 0)
                    self.data.order.PayTypetxt = filter[0].Name;

                var html = template("ordertemp", { address: self.data.address, order: self.data.order, goodslist: self.data.goodslist, logistics: res[4].logistics });
                $("body").html(html);
            })
        },
        sum(arr) {
            var s = 0;
            for (var i = arr.length - 1; i >= 0; i--) {
                s += arr[i];
            }
            return s;
        },
        getorderinfo: function () {
            var self = this;
            return new Promise((resolve, reject) => {
                lobsterh5.GET("/lowcode/APP20200616102057595/shop/?fun=mall.getorderinfo", {
                    orderNo: self.data.orderNo
                }).then(res => {
                    resolve(res);
                }).catch(res => {
                    $.toast(res.msg, 'forbidden');
                    reject(res);
                })
            })
        },
        getrefundlistbyorderno: function () {
            var self = this;
            return new Promise((resolve, reject) => {
                lobsterh5.GET("/lowcode/APP20200616102057595/shop/?fun=mall.getrefundlistbyorderno", {
                    orderNo: self.data.orderNo
                }).then(res => {
                    resolve(res);
                }).catch(res => {
                    $.toast(res.msg, 'forbidden');
                    reject(res);
                })
            })
        },
        getorderaddressbyorderno: function () {
            var self = this;
            return new Promise((resolve, reject) => {
                lobsterh5.GET("/lowcode/APP20200616102057595/shop/?fun=mall.getorderaddressbyorderno", {
                    orderNo: self.data.orderNo
                }).then(res => {
                    resolve(res);
                }).catch(res => {
                    $.toast(res.msg, 'forbidden');
                    reject(res);
                })
            })
        },

        getordergoodslist: function () {
            var self = this;
            return new Promise((resolve, reject) => {
                lobsterh5.GET("/lowcode/APP20200616102057595/shop/?fun=mall.getordergoodslist", {
                    orderNo: self.data.orderNo
                }).then(res => {
                    resolve(res);
                }).catch(res => {
                    $.toast(res.msg, 'forbidden');
                    reject(res);
                })
            })
        },
        //订单物流
        getLogistics: function () {
            var self = this;
            return new Promise((resolve, reject) => {
                lobsterh5.GET("/lowcode/APP20200616102057595/shop/?fun=Mall.GetOrderLogistics", {
                    orderNo: self.data.orderNo
                }).then(res => {
                    resolve(res);
                }).catch(res => {
                    $.toast(res.msg, 'forbidden');
                    reject(res);
                })
            });
        },
    })
</script>